<script>
    var RoleColumn = [
        {
            width: 60,
            title: '编号',
            scopedSlots: { customRender: 'index' },
        },
        {
            width: 150,
            title: '权限组名称',
            dataIndex: 'title',
        },
        {
            width: 150,
            title: '操作时间',
            dataIndex: 'created_at',
            scopedSlots: { customRender: 'time' },
        },
        {
            width: 60,
            title: '状态',
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' },
        },
        {
            width: 100,
            title: '操作',
            dataIndex: 'operations',
            scopedSlots: {
                customRender: 'operations'
            }
        },
    ];

    var RoleOperate = {
        data: {
            RoleColumn,
            MenusData:[],
            title:'添加权限组',
            visible: false,             //添加修改权限组侧栏可见性控制
            RoleList:[],                //权限组列表
            SetRoleRequest:{            //权限组信息，用于提交请求
                id : 0,
                title:'',
                mode:0,
                power:[],
                remark:''
            },
            DefaultSetRoleRequest:{
                id : 0,
                title:'',
                mode:0,
                power:[],
                remark:''
            },
        },
        methods: {
            //页面初始化调用
            initRoles(){
                this.getRolesList()
                this.getSelectMenus()
                this.loadAsideDefaultOpenKeys()
            },

            menusChange(value, label, extra){
                console.log(value)
                console.log(label)
                console.log(extra)
            },

            //显示权限组编辑侧栏
            //isEdit = 1 时为修改
            showRoleOperate(isEdit) {
                if (isEdit > 0) {
                    //修改
                    this.title = '修改权限组'
                } else {
                    this.SetRoleRequest = JSON.parse(JSON.stringify(this.DefaultSetRoleRequest))
                    this.title = '添加权限组'
                }
                this.visible = true;
            },

            //获取父级菜单集合 用于Select（选择父级）
            getSelectMenus() {
                axios.post('/role-set/menus-tree').then(res => {
                    if (res.data.ok) {
                        this.MenusData  = res.data.list
                        console.log(this.MenusData)
                    } else {
                        error('加载失败...')
                    }
                })
            },

            //隐藏权限组编辑侧栏
            hideRoleOperate() {
                this.visible = false;
            },

            //获取权限组信息
            getRolesList(pid=0){
                axios.post('/role-set/get-list', { pid: pid}).then(res => {
                    if (res.data.ok) {
                        this.RoleList = res.data.list
                    } else {
                        error('加载失败...')
                    }
                })
            },

            //提交确认
            RoleConfirm() {
                let verify = this.verifyRequestParam()
                if (!verify) {
                    return
                }
                let param = JSON.parse(JSON.stringify(this.SetRoleRequest))
                param.power = param.power.join(',')
                axios.post('/role-set/save', param).then(res => {
                    if (res.data.ok) {
                        success(res.data.msg)
                        this.SetRoleRequest = JSON.parse(JSON.stringify(this.DefaultSetRoleRequest))
                        this.hideRoleOperate()
                        this.initRoles()
                    } else {
                        error('操作失败: ' + res.data.msg)
                    }
                })

            },

            //权限组启用禁用
            statusUpdate(item,status){
                console.log(item)
                console.log(status)
                axios.post('/role-set/update-status', { id: item.id,status:status}).then(res => {
                    if (res.data.ok) {
                        item.status = status
                        success('操作完成')
                    } else {
                        error(res.data.msg)
                    }
                })
            },

            //删除
            deleteConfirm(item,index) {
                let _this = this
                this.$confirm({
                    title: '系统提示',
                    content:  '确认删除该权限组',
                    onOk() {
                        _this.delete(item,index)
                    }
                })
            },

            delete(item,index){
                axios.post('/role-set/delete', { id: item.id}).then(res => {
                    if (res.data.ok) {
                        this.RoleList.splice(index,1)
                        success('操作完成')
                    } else {
                        error(res.data.msg)
                    }
                })
            },

            //修改
            editRole(item) {
                let _item =  JSON.parse(JSON.stringify(item))
                _item.power = item.power.split(',')
                console.log(_item)
                this.SetRoleRequest = _item
                this.showRoleOperate(1)
            },

            //参数验证
            verifyRequestParam(){
                try {
                    if (this.SetRoleRequest.title == '') {
                        throw '请输入权限组名称'
                    }

                    if (this.SetRoleRequest.power.length < 1) {
                        throw '请选择权限'
                    }

                    return true

                } catch (e) {
                    error(e);
                    return false
                }
            }
        },
    };
</script>